package com.smile.chatview;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;

public class ChatViewButton extends BasicButtonUI {

	private boolean isSelected = false;
	private Color[] color = { new Color(48, 57, 66), new Color(54, 63, 72) };
	private BufferedImage[] images;

	public ChatViewButton(String[] imgNames) {
		super();
		try {
			images = new BufferedImage[2];
			images[0] = ImageIO.read(new File("resources/img/" + imgNames[0] + ".png"));
			images[1] = ImageIO.read(new File("resources/img/" + imgNames[1] + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setSelect(boolean is) {
		this.isSelected = is;
	}

	public void installUI(JComponent c) {
		super.installUI(c);

		AbstractButton button = (AbstractButton) c;
		button.setRolloverEnabled(true);
		button.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	}

	public void paint(Graphics g, JComponent c) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		if (isSelected) {
			g2d.setColor(color[1]);
		} else {
			g2d.setColor(color[0]);
		}
		g2d.fillRect(0, 0, c.getWidth(), c.getHeight());
		
		if (isSelected) {
			if (images[1] != null) {
				g2d.drawImage(images[1], c.getWidth() / 2 - 20, 5, 40, 40, null);
			}
		} else {
			if (images[0] != null) {
				g2d.drawImage(images[0], c.getWidth() / 2 - 20, 5, 40, 40, null);
			}
		}

		super.paint(g, c);
	}

	
	
	@Override
	protected void paintButtonPressed(Graphics g, AbstractButton b) {

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(color[1]);
		g2d.fillRect(0, 0, b.getWidth(), b.getHeight());

		if (images[1] != null) {
			g2d.drawImage(images[1], b.getWidth() / 2 - 20, 5, 40, 40, null);
		}

		super.paintButtonPressed(g, b);
	}
}
